//
//  ReadDetailViewController.m
//  SuperNews
//
//  Created by dllo on 16/1/13.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "ReadDetailViewController.h"

@interface ReadDetailViewController ()

@property (nonatomic, strong) UIWebView *webV;
@property (nonatomic, strong) NSMutableArray *imgArr;
@property (nonatomic, assign) NSInteger height;
@property (nonatomic, copy) NSString *title1;
@property (nonatomic, assign) NSInteger ret;

@end

@implementation ReadDetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.ret = 0;
    self.webV = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.view addSubview:self.webV];
    [self getData];
}

- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.mId];
    [AFNetTool getNetWithURL:str body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        // 数组初始化
        self.imgArr = [NSMutableArray array];
        
        // 获取字典
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[responseObject objectForKey:self.mId]];
        // 可变字符串
        NSMutableString *bodyStr = [NSMutableString string];
        // 获取内容
        bodyStr = [dic objectForKey:@"body"];
        
        // 数组初始化
        NSMutableArray *tempArr = [NSMutableArray array];
        
        // 判断 将图片数据存入数组
        if ([[dic allKeys] containsObject:@"img"]) {
            tempArr = [dic objectForKey:@"img"];
            // 遍历数组
            for (NSDictionary *refDic in tempArr) {
                
                // 遍历 获取key
                for (NSString *key in refDic) {
                    /* 图片自适应 */
                    // 从pixel中取出数据
                    if ([[refDic  allKeys]containsObject:@"pixel"] && ![[refDic valueForKey:@"pixel"] isEqualToString:@""]) {
                        // 获取数据
                        NSString *pixelS = [NSString stringWithFormat:@"%@",[refDic objectForKey:@"pixel"]];
                        
                        // 分解字符串(x*y)
                        NSArray *pixelArr = [pixelS componentsSeparatedByString:@"*"];
                        // 分别取出x,y
                        NSString *widthS = [pixelArr firstObject];
                        NSString *heightS = [pixelArr lastObject];
                        // 分别转成Integer 类型
                        NSInteger width = widthS.integerValue;
                        NSInteger height = heightS.integerValue;
                        // 适配高度
                        self.height = self.view.frame.size.width * height / width;
                    }
                    // 如果没有 高度等于450
                    if (![[refDic allKeys]containsObject:@"pixel"]) {
                        self.height = 450;
                    }
                    // 通过key取出src中的数据
                    if ([key isEqualToString:@"src"]) {
                        
                        // 取出ref
                        NSRange range = [bodyStr rangeOfString:[refDic objectForKey:@"ref"]];
                        
                        // 如果不为空
                        if (range.length != NSNotFound) {
                            
                            // 拼接
                            NSString *picStr = [NSString stringWithFormat:@"<img src=\"%@\" width=\"355\" height=\"%ld\">", [refDic objectForKey:key], self.height];
                            // 拼入
                            bodyStr = [bodyStr stringByReplacingCharactersInRange:range withString:picStr].mutableCopy;
                        }
                    }
                }
            }
        }
        
        NSMutableArray *videoArr = [NSMutableArray array];
        /* 添加视频 */
        if ([[dic allKeys] containsObject:@"video"]) {
            videoArr = [dic objectForKey:@"video"];
            for (NSDictionary *refDic in videoArr) {
                for (NSString *key in refDic) {
                    if ( [key isEqualToString:@"url_mp4"] && ![[refDic valueForKey:@"url_mp4"] isEqualToString:@""] ) {
                        NSRange range1 = [bodyStr rangeOfString:[refDic objectForKey:@"ref"]];
                        if (range1.length != NSNotFound) {
                            NSString *videoStr = [NSString stringWithFormat:@"<video src=\"%@\" width=\"355\" height=\"240\" webkit-playsinline controls=\"preload\"></video>",[refDic objectForKey:key]];
                            bodyStr = [bodyStr stringByReplacingCharactersInRange:range1 withString:videoStr].mutableCopy;
                        }
                    }
                }
            }
        }
        if ([[dic allKeys] containsObject:@"img"]) {
            tempArr = [dic objectForKey:@"img"];
            // 遍历数组
            for (NSDictionary *refDic in tempArr) {
                if ([[refDic allKeys]containsObject:@"pixel"]) {
                    if (0 == self.ret) {
                        // 获取标题
                        self.title1 = [NSString stringWithFormat:@"<div class=\"title\"><H3>%@</H3></div>", [dic objectForKey:@"title"]];
                        if (![self.title1 isEqualToString:@""]) {
                            [bodyStr insertString:self.title1 atIndex:0];
                        }
                        self.ret++;
                    }
                }
            }
        }
        [self.webV loadHTMLString:bodyStr baseURL:nil];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
